package com.bootdo.system.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.system.domain.BCommentsDO;
import com.bootdo.system.service.CommentsService;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;


@Controller
@RequestMapping("/sys/comments")
public class CommentsController extends BaseController{
	@Autowired
	private CommentsService bCommentsService;
	
	
	@GetMapping("/list")
	@ResponseBody
	@ApiOperation(value="评论查询", notes="", response=String.class)
	@ApiImplicitParams({
        @ApiImplicitParam(name = "ownerId", value = "课程id", required = false,paramType="query", dataType = "Long"),
        @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true,paramType="query", dataType = "Integer"),
        @ApiImplicitParam(name = "pageNum", value = "当前页数", required = true,paramType="query", dataType = "Integer")
	})
	R list(@RequestParam Map<String, Object> params) throws Exception{
		Query query = new Query(params);
		List<BCommentsDO> address = bCommentsService.list(query);
		PageInfo<BCommentsDO> pageInfo = new PageInfo<BCommentsDO>(address);
		int total = (int)pageInfo.getTotal();
		PageUtils pageUtil = new PageUtils(address, total);
		return new R("list",pageUtil);
	}
	
	@GetMapping("/edit")
	@ApiOperation(value="评论查询", notes="", response=String.class)
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "评论id", required = false,paramType="query", dataType = "Long")
	})
	R edit(Long id)  throws Exception{
		return new R("obj",bCommentsService.get(id));
	}
	
	/**
	 * 保存
	 */
	@ApiOperation(value="评论保存", notes="保存评论", response=String.class)
	@PostMapping("/save")
	@ResponseBody
	R save(@RequestBody BCommentsDO bComments) throws Exception{
		bComments.setCreateTime(new Date());
		try {
			if (bCommentsService.save(bComments) > 0) {
				return R.saveOk(bComments.getId().toString());
			} else {
				return R.error(1, "保存失败");
			}
		}catch(Exception e) {
			e.printStackTrace();
			return R.error(1, "保存失败");
		}
	}
	
	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperation(value="评论更新", notes="保存更新", response=String.class)
	@ResponseBody
	R update(@RequestBody  BCommentsDO bComments) throws Exception{
		if (bCommentsService.update(bComments) > 0) {
			return R.ok();
		} else {
			return R.error(1, "更新失败");
		}
	}
	
	@ApiOperation(value="评论删除", notes="", response=String.class)
	@GetMapping( "/remove")
	@ResponseBody
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "评论id", required = false,paramType="query", dataType = "Long")
	})
	R remove( Integer id) throws Exception{
		if(bCommentsService.remove(id)>0){
			return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	R delete(@RequestParam("ids[]") Integer[] ids) throws Exception{
		bCommentsService.batchRemove(ids);
		return R.ok();
	}
	
}
